USE [NANS21V]
GO

/****** Object:  StoredProcedure [dbo].[TukaKiroku]    Script Date: 06/03/2014 19:50:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TukaKiroku]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TukaKiroku]
GO

CREATE PROCEDURE [dbo].[TukaKiroku]
	@TAIKAINO int,
	@KYOGINO int,
	@SUBNO int,
	@ROUNDNO int,
	@SUBSYUMOKUKUBUN int,
	@SUBKIROKUKUBUN int
AS
SELECT DISTINCT
	dbo.Kiroku(RO.TUKAKIROKU, @SUBSYUMOKUKUBUN, @SUBKIROKUKUBUN, (CASE WHEN CONVERT(varchar, RE.EXTEND1) = '1' then 3 else RE.EXTEND2 END)) AS KIROKU,
	RO.TUKAKIROKU
FROM
	TBL_ROUND AS RO,
	TBL_RESULT AS RE
WHERE
	RO.TAIKAINO = RE.TAIKAINO
	AND RO.KYOGINO = RE.KYOGINO
	AND RO.SUBNO = RE.SUBNO
	AND RO.ROUNDNO = RE.ROUNDNO
	AND RO.TAIKAINO = @TAIKAINO
	AND RO.KYOGINO = @KYOGINO
	AND RO.SUBNO = @SUBNO
	AND RO.ROUNDNO = @ROUNDNO
GO

